
import { getTime } from './common'

const errorList = []

// 捕获资源加载等错误
window.addEventListener('error', error => {
  const { target } = error;
  if (target !== window) {
    errorList.push({
      type: target.localName,
      url: target.src || target.href,
      message: (target.src || target.href) + ' is load error',
      url: window.location.href,
      ...getTime()
    })
  }
}, true)

// 捕获js错误
window.onerror = (msg, url, row, col, error) => {
  errorList.push({
    type: 'javascript',
    url: window.location.href,
    row, // 无法捕获正确row
    col, // 无法捕获正确col
    message: error,
    ...getTime()
  })
}

// 捕获promise错误
window.addEventListener('unhandledrejection', error => {
  errorList.push({
    type: 'promise error',
    message: 'promise error reason:' + error.reason,
    ...getTime()
  })
})